package cn.edu.neusoft.wuguangsheng.market.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import cn.edu.neusoft.wuguangsheng.market.R;
import cn.edu.neusoft.wuguangsheng.market.fragment.CartFragment;
import cn.edu.neusoft.wuguangsheng.market.fragment.MyFragment;
import cn.edu.neusoft.wuguangsheng.market.fragment.OrderFragment;
import cn.edu.neusoft.wuguangsheng.market.fragment.ShopFragment;

public class MainActivity extends BaseActivity implements View.OnClickListener{
    LinearLayout tab_sy;
    LinearLayout tab_dd;
    LinearLayout tab_gwc;
    LinearLayout tab_wd;

    ImageView img_sy;
    ImageView img_dd;
    ImageView img_gwc;
    ImageView img_wd;

    Fragment fragment_shop;
    Fragment fragment_order;
    Fragment fragment_cart;
    Fragment fragment_my;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_main);
        tab_sy = (LinearLayout) findViewById(R.id.tab_sy);
        tab_dd = (LinearLayout) findViewById(R.id.tab_dd);
        tab_gwc = (LinearLayout) findViewById(R.id.tab_gwc);
        tab_wd = (LinearLayout) findViewById(R.id.tab_wd);

        img_sy=(ImageView)findViewById(R.id.img_sy);
        img_dd=(ImageView)findViewById(R.id.img_dd);
        img_gwc=(ImageView)findViewById(R.id.img_gwc);
        img_wd=(ImageView)findViewById(R.id.img_wd);


        selectTab(0);
    }

    @Override
    void initEvent() {
        tab_sy.setOnClickListener(this);
        tab_dd.setOnClickListener(this);
        tab_gwc.setOnClickListener(this);
        tab_wd.setOnClickListener(this);
    }

    @Override
    void initDate() {

    }

    @Override
    public void onClick(View v) {
       resetImage();
   switch (v.getId()){
    case R.id.tab_sy:
        selectTab(0);
        break;
    case R.id.tab_dd:
        selectTab(1);
        break;
    case R.id.tab_gwc:
        selectTab(2);
        break;
    case R.id.tab_wd:
        selectTab(3);
        break;
    default:
        break;
}
    }
private void resetImage(){
    img_sy.setImageResource(R.drawable.syh);
    img_dd.setImageResource(R.drawable.ddh);
    img_gwc.setImageResource(R.drawable.gwch);
    img_wd.setImageResource(R.drawable.wdh);
}

private void selectTab(int i){
fragmentManager=getSupportFragmentManager();
    fragmentTransaction=fragmentManager.beginTransaction();
    hideFragment(fragmentTransaction);
    switch (i){
        case 0:
            if (fragment_shop==null){
                fragment_shop=new ShopFragment();
                fragmentTransaction.add(R.id.content,fragment_shop);
            }else{
                fragmentTransaction.show(fragment_shop);
            }

            img_sy.setImageResource(R.drawable.syl);
            break;
        case 1:
            if (fragment_order==null){
                fragment_order=new OrderFragment();
                fragmentTransaction.add(R.id.content,fragment_order);
            }else{

                fragmentTransaction.show(fragment_order);
            }
            img_dd.setImageResource(R.drawable.ddl);
            break;
        case 2:
            if (fragment_cart==null){
                fragment_cart=new CartFragment();
                fragmentTransaction.add(R.id.content,fragment_cart);
            }else{
                fragmentTransaction.show(fragment_cart);
            }
            img_gwc.setImageResource(R.drawable.gwcl);
            break;
        case 3:
            if (fragment_my==null){
                fragment_my=new MyFragment();
                fragmentTransaction.add(R.id.content,fragment_my);
            }else{
                fragmentTransaction.show(fragment_my);
            }
            img_wd.setImageResource(R.drawable.wdl);
            break;
        default:
            break;
    }
    fragmentTransaction.commit();
}
    private void hideFragment(FragmentTransaction fragmentTransaction){
        if (fragment_shop!=null){
            fragmentTransaction.hide(fragment_shop);
        }
        if (fragment_order!=null){
            fragmentTransaction.hide(fragment_order);
        }
        if (fragment_cart!=null){
            fragmentTransaction.hide(fragment_cart);
        }
        if (fragment_my!=null){
            fragmentTransaction.hide(fragment_my);
        }
    }



}